package com.share.device.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.share.device.domain.vo.WorkOrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.device.mapper.WorkOrderMapper;
import com.share.device.domain.WorkOrder;
import com.share.device.service.IWorkOrderService;

/**
 * 工单信息Service业务层处理
 * 
 * @author         
 * @date 2025-06-29
 */
@Service
public class WorkOrderServiceImpl extends ServiceImpl<WorkOrderMapper, WorkOrder> implements IWorkOrderService 
{
    @Autowired
    private WorkOrderMapper workOrderMapper;

    /**
     * 查询工单信息
     * 
     * @param orderId 工单信息主键
     * @return 工单信息
     */
    @Override
    public WorkOrder selectWorkOrderById(Integer orderId)
    {
        return workOrderMapper.selectWorkOrderById(orderId);
    }

    /**
     * 查询工单信息列表
     *
     * @param workOrder 工单信息
     * @return 工单信息
     */
    @Override
    public List<WorkOrderVO> selectWorkOrderList(WorkOrder workOrder)
    {
        return workOrderMapper.selectWorkOrderList(workOrder);
    }

    /**
     * 新增工单信息
     * 
     * @param workOrder 工单信息
     * @return 结果
     */
    @Override
    public int insertWorkOrder(WorkOrder workOrder)
    {
        // 设置默认状态为待处理
        if (workOrder.getStatus() == null) {
            workOrder.setStatus("pending");
        }
        // 设置默认优先级为中
        if (workOrder.getPriority() == null) {
            workOrder.setPriority("medium");
        }
        workOrder.setCreateTime(new Date());
        return workOrderMapper.insertWorkOrder(workOrder);
    }

    /**
     * 修改工单信息
     * 
     * @param workOrder 工单信息
     * @return 结果
     */
    @Override
    public int updateWorkOrder(WorkOrder workOrder)
    {
        workOrder.setUpdateTime(new Date());
        return workOrderMapper.updateWorkOrder(workOrder);
    }

    /**
     * 批量删除工单信息
     * 
     * @param orderIds 需要删除的工单信息主键
     * @return 结果
     */
    @Override
    public int deleteWorkOrderByIds(Integer[] orderIds)
    {
        return workOrderMapper.deleteWorkOrderByIds(orderIds);
    }

    /**
     * 删除工单信息信息
     * 
     * @param orderId 工单信息主键
     * @return 结果
     */
    @Override
    public int deleteWorkOrderById(Integer orderId)
    {
        return workOrderMapper.deleteWorkOrderById(orderId);
    }

    /**
     * 获取工单详细信息（包含关联信息）
     * 
     * @param orderId 工单ID
     * @return 工单详细信息
     */
    @Override
    public WorkOrder getWorkOrderDetail(Integer orderId)
    {
        return workOrderMapper.selectWorkOrderDetailById(orderId);
    }

    /**
     * 分配工单给处理人
     * 
     * @param orderId 工单ID
     * @param assigneeId 处理人ID
     * @return 结果
     */
    @Override
    public int assignWorkOrder(Integer orderId, Integer assigneeId)
    {
        WorkOrder workOrder = new WorkOrder();
        workOrder.setOrderId(orderId);
        workOrder.setAssigneeId(assigneeId);
        workOrder.setStatus("processing");
        workOrder.setUpdateTime(new Date());
        return workOrderMapper.updateWorkOrder(workOrder);
    }

    /**
     * 更新工单状态
     * 
     * @param orderId 工单ID
     * @param status 新状态
     * @return 结果
     */
    @Override
    public int updateWorkOrderStatus(Integer orderId, String status)
    {
        WorkOrder workOrder = new WorkOrder();
        workOrder.setOrderId(orderId);
        workOrder.setStatus(status);
        workOrder.setUpdateTime(new Date());
        
        // 如果状态为已完成，设置完成时间
        if ("completed".equals(status)) {
            workOrder.setCompleteTime(new Date());
        }
        
        return workOrderMapper.updateWorkOrder(workOrder);
    }

    /**
     * 完成工单
     * 
     * @param orderId 工单ID
     * @return 结果
     */
    @Override
    public int completeWorkOrder(Integer orderId)
    {
        return updateWorkOrderStatus(orderId, "completed");
    }

    /**
     * 根据状态查询工单列表
     * 
     * @param status 工单状态
     * @return 工单信息集合
     */
    @Override
    public List<WorkOrder> selectWorkOrderListByStatus(String status)
    {
        return workOrderMapper.selectWorkOrderListByStatus(status);
    }

    /**
     * 根据处理人查询工单列表
     * 
     * @param assigneeId 处理人ID
     * @return 工单信息集合
     */
    @Override
    public List<WorkOrder> selectWorkOrderListByAssignee(Integer assigneeId)
    {
        return workOrderMapper.selectWorkOrderListByAssignee(assigneeId);
    }

    /**
     * 根据设备ID查询工单列表
     * 
     * @param deviceId 设备ID
     * @return 工单信息集合
     */
    @Override
    public List<WorkOrder> selectWorkOrderListByDevice(Integer deviceId)
    {
        return workOrderMapper.selectWorkOrderListByDevice(deviceId);
    }

    /**
     * 获取工单统计信息
     * 
     * @return 统计信息
     */
    @Override
    public Map<String, Object> getWorkOrderStatistics()
    {
        Map<String, Object> statistics = new HashMap<>();
        
        // 统计各状态的工单数量
        List<WorkOrder> pendingOrders = selectWorkOrderListByStatus("pending");
        List<WorkOrder> processingOrders = selectWorkOrderListByStatus("processing");
        List<WorkOrder> completedOrders = selectWorkOrderListByStatus("completed");
        List<WorkOrder> closedOrders = selectWorkOrderListByStatus("closed");
        
        statistics.put("pendingCount", pendingOrders.size());
        statistics.put("processingCount", processingOrders.size());
        statistics.put("completedCount", completedOrders.size());
        statistics.put("closedCount", closedOrders.size());
        statistics.put("totalCount", pendingOrders.size() + processingOrders.size() + completedOrders.size() + closedOrders.size());
        
        return statistics;
    }
} 